import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

# 生成模拟土壤数据
data = {
    'pH': np.random.uniform(5.0, 8.0, 100),
    'Nitrogen': np.random.uniform(0, 200, 100),
    'Phosphorus': np.random.uniform(0, 50, 100),
    'Potassium': np.random.uniform(0, 100, 100)
}
df = pd.DataFrame(data)

# 执行聚类
kmeans = KMeans(n_clusters=3, random_state=42)
clusters = kmeans.fit_predict(df)

# 可视化聚类结果
plt.figure(figsize=(10,6))
plt.scatter(df['Nitrogen'], df['Phosphorus'], c=clusters, cmap='viridis')
plt.xlabel('Nitrogen Content')
plt.ylabel('Phosphorus Content')
plt.title('Soil Fertility Clustering')
plt.show()